<!DOCTYPE html>
<html lang="zh-CN" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>注册 - milkcode</title>
    <link rel="stylesheet" href="/styles.css">
    <script th:if="${session.user == null}" src="/js/ad.js"></script>
    <style>
        .register-container {
            max-width: 400px;
            margin: 90px auto 0 auto;
            padding: 20px;
            background: #fff;
            border-radius: 8px;
            box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
        }
        @media (max-width: 600px) {
            .register-container {
                margin-top: 180px;
            }
        }
        main {
            margin-top: 0 !important;
            padding-top: 0 !important;
        }

        .register-header {
            text-align: center;
            margin-bottom: 30px;
        }

        .register-form {
            display: flex;
            flex-direction: column;
            gap: 20px;
        }

        .form-group {
            display: flex;
            flex-direction: column;
            gap: 8px;
        }

        .form-group label {
            font-weight: 500;
            color: #333;
        }

        .form-group input {
            padding: 12px;
            border: 1px solid #ddd;
            border-radius: 4px;
            font-size: 16px;
        }

        .form-group input:focus {
            outline: none;
            border-color: var(--primary-color);
            box-shadow: 0 0 0 2px rgba(37, 99, 235, 0.1);
        }

        .register-btn {
            background: linear-gradient(120deg, var(--primary-color), var(--secondary-color));
            color: white;
            padding: 12px;
            border: none;
            border-radius: 4px;
            font-size: 16px;
            cursor: pointer;
            transition: all 0.3s ease;
            position: relative;
            overflow: hidden;
        }

        .register-btn::before {
            content: '';
            position: absolute;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            background: linear-gradient(120deg, var(--secondary-color), var(--primary-color));
            opacity: 0;
            transition: opacity 0.3s ease;
        }

        .register-btn:hover {
            transform: translateY(-2px);
            box-shadow: 0 4px 12px rgba(37, 99, 235, 0.2);
        }

        .register-btn:hover::before {
            opacity: 1;
        }

        .register-btn span {
            position: relative;
            z-index: 1;
        }

        .register-footer {
            text-align: center;
            margin-top: 20px;
        }

        .register-footer a {
            color: var(--primary-color);
            text-decoration: none;
        }

        .register-footer a:hover {
            text-decoration: underline;
        }

        .password-requirements {
            font-size: 0.875rem;
            color: #666;
            margin-top: 4px;
        }

        .password-requirements ul {
            list-style: none;
            padding-left: 0;
            margin-top: 4px;
        }

        .password-requirements li {
            display: flex;
            align-items: center;
            gap: 4px;
            margin-bottom: 2px;
        }

        .password-requirements li::before {
            content: '•';
            color: var(--primary-color);
        }

        .message {
            position: fixed;
            top: 20px;
            left: 50%;
            transform: translateX(-50%);
            padding: 10px 20px;
            border-radius: 4px;
            background-color: #4CAF50;
            color: white;
            font-size: 14px;
            opacity: 0;
            transition: opacity 0.3s ease;
            z-index: 1000;
        }

        .message.show {
            opacity: 1;
        }
    </style>
</head>
<body>
    <header>
        <div class="header-content">
            <h1>milkcode</h1>
            <nav class="header-nav">
                <div class="search-container">
                    <input type="text" class="search-input" placeholder="搜索资源...">
                    <svg class="search-icon" viewBox="0 0 24 24">
                        <path fill="currentColor" d="M15.5 14h-.79l-.28-.27C15.41 12.59 16 11.11 16 9.5 16 5.91 13.09 3 9.5 3S3 5.91 3 9.5 5.91 16 9.5 16c1.61 0 3.09-.59 4.23-1.57l.27.28v.79l5 4.99L20.49 19l-4.99-5zm-6 0C7.01 14 5 11.99 5 9.5S7.01 5 9.5 5 14 7.01 14 9.5 11.99 14 9.5 14z"/>
                    </svg>
                </div>
                <div class="nav-buttons">
                    <a href="/" class="nav-link">返回首页</a>
                    <a href="login" class="login-btn">登录去广告</a>
                    <a href="contact" class="contact-btn">
                        <span>联系我们</span>
                        <svg class="contact-icon" viewBox="0 0 24 24" width="18" height="18">
                            <path fill="currentColor" d="M20 4H4c-1.1 0-2 .9-2 2v12c0 1.1.9 2 2 2h16c1.1 0 2-.9 2-2V6c0-1.1-.9-2-2-2zm0 4l-8 5-8-5V6l8 5 8-5v2z"/>
                        </svg>
                    </a>
                </div>
            </nav>
        </div>
    </header>

    <main>
        <div id="message" class="message"></div>
        <div class="register-container">
            <div class="register-header">
                <h2>创建账号</h2>
            </div>
            <form class="register-form" id="registerForm" onsubmit="return handleRegister(event)">
                <div class="form-group">
                    <label for="userName">用户名</label>
                    <input type="text" id="userName" name="userName" required placeholder="请输入用户名">
                </div>
                <div class="form-group">
                    <label for="email">电子邮箱</label>
                    <input type="email" id="email" name="email" required placeholder="请输入电子邮箱">
                </div>
                <div class="form-group">
                    <label for="password">密码</label>
                    <input type="password" id="password" name="password" required placeholder="请输入密码">
                </div>
                <div class="form-group">
                    <label for="confirm-password">确认密码</label>
                    <input type="password" id="confirm-password" name="confirm-password" required placeholder="请再次输入密码">
                </div>
                <button type="submit" class="register-btn">
                    <span>注册</span>
                </button>
            </form>
            <div class="register-footer">
                <p>已有账号？<a href="login">立即登录去广告</a></p>
            </div>
        </div>
    </main>

    <footer>
        <p>© 2024 milkcode. All rights reserved.</p>
    </footer>

    <script>
        function showMessage(text, isSuccess = true) {
            const message = document.getElementById('message');
            message.textContent = text;
            message.style.backgroundColor = isSuccess ? '#4CAF50' : '#f44336';
            message.classList.add('show');
            
            setTimeout(() => {
                message.classList.remove('show');
                if (isSuccess) {
                    window.location.href = '/login';
                }
            }, 1500);
        }

        async function handleRegister(event) {
            event.preventDefault();
            
            const form = document.getElementById('registerForm');
            const userName = form.userName.value.trim();
            const email = form.email.value.trim();
            const password = form.password.value;
            const confirmPassword = form['confirm-password'].value;

            // 前端验证
            if (!userName || !email || !password || !confirmPassword) {
                showMessage('请填写所有必填字段', false);
                return false;
            }

            if (password !== confirmPassword) {
                showMessage('两次输入的密码不一致', false);
                return false;
            }

            try {
                const response = await fetch('/api/register', {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json',
                    },
                    body: JSON.stringify({
                        userName,
                        email,
                        password
                    })
                });

                const result = await response.json();
                
                if (result.success) {
                    showMessage('注册成功');
                } else {
                    showMessage(result.message || '注册失败，请稍后重试', false);
                }
            } catch (error) {
                console.error('注册请求失败:', error);
                showMessage('注册请求失败，请稍后重试', false);
            }

            return false;
        }
    </script>
</body>
</html> 